﻿using System;
using System.Net;
using System.Text;

namespace CatUtilsDll.Utils.Currency.WebUtils
{
    public partial class WebHelper
    {
        #region Http Get 异步

        /// <summary>
        /// Http异步请求
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="webHeader"></param>
        /// <param name="callBackDownStringCompleted"></param>
        /// <param name="CallProgressCount"></param>
        public static void HttpGetAsync(string RequestUrl, WebHeaderCollection webHeader,
        DownloadStringCompletedEventHandler callBackDownStringCompleted, DownloadProgressChangedEventHandler CallProgressCount)
        {
            HttpGetAsync(RequestUrl, webHeader, callBackDownStringCompleted, CallProgressCount, Encoding.UTF8);
        }

        /// <summary>
        /// Http异步请求
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="callBackDownStringCompleted"></param>
        /// <param name="CallProgressCount"></param>
        public static void HttpGetAsync(string RequestUrl,
        DownloadStringCompletedEventHandler callBackDownStringCompleted, DownloadProgressChangedEventHandler CallProgressCount)
        {
            HttpGetAsync(RequestUrl, WebHelper.PcWebHeader(), callBackDownStringCompleted, CallProgressCount, Encoding.UTF8);
        }

        /// <summary>
        /// Http异步请求
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="callBackDownStringCompleted"></param>
        /// <param name="CallProgressCount"></param>
        /// <param name="encode"></param>
        public static void HttpGetAsync(string RequestUrl,
        DownloadStringCompletedEventHandler callBackDownStringCompleted, DownloadProgressChangedEventHandler CallProgressCount, Encoding encode)
        {
            HttpGetAsync(RequestUrl, WebHelper.PcWebHeader(), callBackDownStringCompleted, CallProgressCount, encode);
        }

        /// <summary>
        /// Http异步请求
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="headerCollection"></param>
        /// <param name="callBackDownStringCompleted"></param>
        /// <param name="CallProgressCount"></param>
        /// <param name="encode"></param>
        public static void HttpGetAsync(string RequestUrl, WebHeaderCollection headerCollection,
            DownloadStringCompletedEventHandler callBackDownStringCompleted, DownloadProgressChangedEventHandler CallProgressCount, Encoding encode)
        {
            using (WebClient webClient = new WebClient())
            {
                webClient.Headers = headerCollection;
                webClient.Encoding = encode;
                webClient.DownloadStringCompleted += callBackDownStringCompleted;

                webClient.DownloadProgressChanged += CallProgressCount;
                try
                {
                    webClient.DownloadStringAsync(new Uri(RequestUrl));
                }
                catch (Exception ex)
                {
                    PrintWebLog(RequestUrl, ex);
                }
            }
        }

        #endregion Http Get 异步

        #region Http Post 异步

        /// <summary>
        /// 发送Post请求并返回结果，发送时使用UTF8编码
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="PostData"></param>
        /// <param name="downloadDataCompleted"></param>
        public static void HttpPostAsync(string RequestUrl, string PostData, DownloadDataCompletedEventHandler downloadDataCompleted)
        {
            HttpPostAsync(RequestUrl, PostData, downloadDataCompleted, Encoding.UTF8);
        }

        /// <summary>
        /// 发送Post请求并返回结果，需要自行配置发送数据读取编码
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="PostData"></param>
        /// <param name="downloadDataCompleted"></param>
        /// <param name="encoding">配置发送数据使用的编码</param>
        public static void HttpPostAsync(string RequestUrl, string PostData, DownloadDataCompletedEventHandler downloadDataCompleted, Encoding encoding)
        {
            WebHeaderCollection webHeaderCollection = WebHelper.PcWebHeader(RequestUrl);

            webHeaderCollection.Add("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
            HttpPostAsync(RequestUrl, PostData, webHeaderCollection, downloadDataCompleted, encoding);
        }

        /// <summary>
        /// 发送Post请求并返回结果，发送时使用UTF8编码
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="PostData"></param>
        /// <param name="webHeaderCollection"></param>
        /// <param name="downloadDataCompleted"></param>
        public static void HttpPostAsync(string RequestUrl, string PostData, WebHeaderCollection webHeaderCollection, DownloadDataCompletedEventHandler downloadDataCompleted)
        {
            HttpPostAsync(RequestUrl, PostData, webHeaderCollection, downloadDataCompleted, Encoding.UTF8);
        }

        /// <summary>
        /// 发送Post请求并返回结果，需要自行配置发送数据读取编码
        /// </summary>
        /// <param name="RequestUrl"></param>
        /// <param name="PostData"></param>
        /// <param name="webHeaderCollection"></param>
        /// <param name="downloadDataCompleted"></param>
        /// <param name="encoding"></param>
        public static void HttpPostAsync(string RequestUrl, string PostData, WebHeaderCollection webHeaderCollection, DownloadDataCompletedEventHandler downloadDataCompleted, Encoding encoding)
        {
            using (WebClient PostWebClient = new WebClient())
            {
                PostWebClient.Headers = webHeaderCollection;
                try
                {
                    byte[] byteArray = encoding.GetBytes(PostData);
                    PostWebClient.DownloadDataCompleted += downloadDataCompleted;
                    PostWebClient.UploadDataAsync(new Uri(RequestUrl), byteArray);
                }
                catch (Exception ex)
                {
                    PrintWebLog(RequestUrl, ex);
                }
            }
        }

        #endregion Http Post 异步
    }
}